package com.weaver.openapi.service;

import com.weaver.openapi.pojo.crm.params.*;
import com.weaver.openapi.util.*;
import java.util.*;
import com.alibaba.fastjson.*;
import cn.hutool.http.*;
import com.weaver.openapi.pojo.crm.res.customer.*;
import com.weaver.openapi.pojo.*;

public class CrmCustomerService
{
    public static NormalRes<CrmCustomerResultVo> createCrmCustomerV2(final CrmVo crmVo, String host, final Map<String, String> headers) {
        host = HostUtil.beforeRequestCheckHeaders(host, headers, "application/x-www-form-urlencoded");
        final Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("access_token", crmVo.getAccessToken());
        paramMap.put("userid", crmVo.getUserid());
        paramMap.put("module", crmVo.getModule());
        paramMap.put("entity", JSONObject.toJSONString((Object)crmVo.getCustomerEntity()));
        final String resJson = ((HttpRequest)HttpRequest.post(host + "/crm/v2/create").form((Map)paramMap).headerMap((Map)headers, true)).execute().body();
        return transferResult(resJson);
    }
    
    public static MessageResult deleteCrmCustomerV2(final CrmVo crmVo, String host, final Map<String, String> headers) {
        host = HostUtil.beforeRequestCheckHeaders(host, headers, "application/x-www-form-urlencoded");
        final Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("access_token", crmVo.getAccessToken());
        paramMap.put("userid", crmVo.getUserid());
        paramMap.put("module", crmVo.getModule());
        paramMap.put("id", crmVo.getId());
        final String resJson = ((HttpRequest)HttpRequest.post(host + "/crm/v2/delete").form((Map)paramMap).headerMap((Map)headers, true)).execute().body();
        return (MessageResult)JSONObject.parseObject(resJson, (Class)MessageResult.class);
    }
    
    public static NormalRes<CrmCustomerResultVo> updateCrmCustomerV2(final CrmVo crmVo, String host, final Map<String, String> headers) {
        host = HostUtil.beforeRequestCheckHeaders(host, headers, "application/x-www-form-urlencoded");
        final Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("access_token", crmVo.getAccessToken());
        paramMap.put("userid", crmVo.getUserid());
        paramMap.put("module", crmVo.getModule());
        paramMap.put("entity", JSONObject.toJSONString((Object)crmVo.getCustomerEntity()));
        final String resJson = ((HttpRequest)HttpRequest.post(host + "/crm/v2/update").form((Map)paramMap).headerMap((Map)headers, true)).execute().body();
        return transferResult(resJson);
    }
    
    public static NormalRes<CrmCustomerResultVo> getCrmCustomerV2(final CrmVo crmVo, String host, final Map<String, String> headers) {
        host = HostUtil.beforeRequestCheckHeaders(host, headers, "application/x-www-form-urlencoded");
        final String resJson = ((HttpRequest)HttpRequest.get(host + String.format("/crm/v2/get?access_token=%s&userid=%s&module=%s&id=%s", crmVo.getAccessToken(), crmVo.getUserid(), crmVo.getModule(), crmVo.getId())).headerMap((Map)headers, true)).execute().body();
        return transferResult(resJson);
    }
    
    public static NormalRes<CrmCustomerSearchResultVo> searchCrmCustomerV2(final CrmVo crmVo, String host, final Map<String, String> headers) {
        host = HostUtil.beforeRequestCheckHeaders(host, headers, "application/x-www-form-urlencoded");
        final String resJson = ((HttpRequest)HttpRequest.get(host + String.format("/crm/v2/search?access_token=%s&userid=%s&module=%s&query=%s", crmVo.getAccessToken(), crmVo.getUserid(), crmVo.getModule(), JSONObject.toJSONString((Object)crmVo.getQuery()))).headerMap((Map)headers, true)).execute().body();
        return transferSearchResult(resJson);
    }
    
    public static NormalRes<CrmCustomerResultVo> transferResult(final String resJson) {
        final JSONObject jsonObject = JSONObject.parseObject(resJson);
        final CrmCustomerResultVo data = (CrmCustomerResultVo)jsonObject.getObject("data", (Class)CrmCustomerResultVo.class);
        final NormalResult message = (NormalResult)jsonObject.getObject("message", (Class)NormalResult.class);
        final NormalRes<CrmCustomerResultVo> normalRes = new NormalRes<CrmCustomerResultVo>();
        normalRes.setData(data);
        normalRes.setMessage(message);
        return normalRes;
    }
    
    public static NormalRes<CrmCustomerSearchResultVo> transferSearchResult(final String resJson) {
        final JSONObject jsonObject = JSONObject.parseObject(resJson);
        final CrmCustomerSearchResultVo data = (CrmCustomerSearchResultVo)jsonObject.getObject("data", (Class)CrmCustomerSearchResultVo.class);
        final NormalResult message = (NormalResult)jsonObject.getObject("message", (Class)NormalResult.class);
        final NormalRes<CrmCustomerSearchResultVo> normalRes = new NormalRes<CrmCustomerSearchResultVo>();
        normalRes.setData(data);
        normalRes.setMessage(message);
        return normalRes;
    }
}
